package com.cgc.shop.action;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.HashMap;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cgc.shop.model.ImageFile;
import com.cgc.shop.model.Product;
import com.cgc.shop.utils.FileUploadUtil;

@Controller("productAction")
@Scope("prototype")
public class ProductAction extends BaseAction<Product>{
	
	private ImageFile imageFile;
	
	public String queryJoinCategory() {
		jsonMap = new HashMap<String, Object>();
		System.out.println("page=" + page);
		System.out.println("rows=" + rows);
		jsonMap.put("total", productService.getCount(model.getName()));
		jsonMap.put("rows", productService.queryJoinCategory(model.getName(), page, rows));
		return "queryJoinCategory";
	}
	
	public String deleteByIds() {
		productService.deleteByIds(ids);
		inputStream = new ByteArrayInputStream("success".getBytes());
		return "deleteByIds";
	}
	
	public void save() {
		model.setPic(FileUploadUtil.upload(getImageFile()));
		model.setDate(new Date());
		productService.save(model);
	}
	
	public void update() {
		model.setPic(FileUploadUtil.upload(getImageFile()));
		model.setDate(new Date());
		productService.update(model);
	}
	
	public String get() {
		requestMap.put("product", productService.get(model.getId()));
		return "get";
	}

	public ImageFile getImageFile() {
		return imageFile;
	}

	public void setImageFile(ImageFile imageFile) {
		this.imageFile = imageFile;
	}
}
